﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;


namespace order_project
{
    public class OrderService
    {
        public List<Order> orders = new List<Order>();

        // xml序列化和反序列化
        public void Export(string filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream stream = new FileStream(filename, FileMode.Create))
            {
                serializer.Serialize(stream, orders);
            }
        }

        public void Import(string filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream stream = new FileStream(filename, FileMode.Open))
            {
                orders = (List<Order>)serializer.Deserialize(stream);
            }
        }

        public void AddOrder(Order order)
        {
            if (!orders.Contains(order))
            {
                orders.Add(order);
            }
            else
            {
                throw new Exception("Order already exists.");
            }
        }

        public void RemoveOrder(int orderId)
        {
            var order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order != null)
            {
                orders.Remove(order);
            }
            else
            {
                throw new Exception("Order not found.");
            }
        }

        public void ModifyOrder(int orderId, Order newOrder)
        {
            var order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order != null)
            {
                orders.Remove(order);
                orders.Add(newOrder);
            }
            else
            {
                throw new Exception("Order not found.");
            }
        }

        public IEnumerable<Order> QueryOrdersByCustomer(string customer)
        {
            return orders.Where(o => o.Customer == customer).OrderByDescending(o => o.TotalAmount());
        }

        public IEnumerable<Order> QueryOrdersByProduct(string productName)
        {
            return orders.Where(o => o.OrderDetails.Any(d => d.ProductName == productName)).OrderByDescending(o => o.TotalAmount());
        }

        public IEnumerable<Order> QueryOrdersByAmount(decimal amount)
        {
            return orders.Where(o => o.TotalAmount() >= amount).OrderByDescending(o => o.TotalAmount());
        }

        public void SortOrders()
        {
            orders.Sort((x, y) => x.OrderId.CompareTo(y.OrderId));
        }

        public void ClearOrders()
        {
            orders.Clear();
        }

        public void ShowOrders()
        {
            SortOrders();
            foreach (var order in orders)
            {
                Console.WriteLine(order);
            }
        }  


        public void CustomSort(Func<Order, object> sortPredicate)
        {
            orders = orders.OrderBy(sortPredicate).ToList();
        }
    }

}
